package atom.core0.util;

import java.util.concurrent.atomic.AtomicInteger;

import atom.core0.global.GlobalCache;


public class RandomUtil
{
	private static AtomicInteger count = new AtomicInteger(0);
	//XXXXX13983311914689999=28位
	//10+13+"_"+4(1毫秒产生多少个)=28位
	public static String genRandomUUID()
	{

		int index = count.addAndGet(1);
		if (index >= 10000 )
		{	
			setZero();
			index = count.addAndGet(1);
		}
		
		//RandomStringUtils.randomNumeric(4)
		//String s1=StringUtil.getString(GlobalCache.getNodeno(),10);
//		String s1=GlobalCache.getNodeno();
//		String s2=String.format("%013d",System.currentTimeMillis());
//		String uuid = s1+s2+"_"+String.format("%04d",index);
		String uuid = GlobalCache.getNodeno()+System.currentTimeMillis()+"_"+String.format("%04d",index);
		return uuid;
	}
	synchronized public static void setZero()
	{
		if (count.get()>=10000)
			count.set(0);
	}
	public static String genRandomUUID(String nodeno)
	{

		int index = count.addAndGet(1);
		if (index >= 10000 )
		{	
			setZero();
			index = count.addAndGet(1);
		}
		
		String uuid = nodeno+System.currentTimeMillis()+"_"+String.format("%04d",index);
		return uuid;
	}
	
}
